package view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import dao.OrdersDao;
import modle.Country;
import modle.Orders;
import util.DbUtil;
import util.StringUtil;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import java.awt.Toolkit;

public class Feedback extends JFrame {

	private JPanel contentPane;
	private JButton btnSubmit;
	private JTextArea textArea;
	private JButton btnBack;
	private JLabel Label_1;
	private JLabel Label_2;
	private int Id, Num;
	private JComboBox comboBox;
	private List<Country> listCountries;
	private Locale locale;
	ResourceBundle rd;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Feedback frame = new Feedback(1, 1);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Feedback(int num, int id) {
		setIconImage(Toolkit.getDefaultToolkit().getImage(Feedback.class.getResource("/img/iconImage.jpg")));
		setTitle("Welcome to Food Corner");
		
		Num = num;
		Id = id;
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 582, 657);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		 Label_1 = new JLabel("Dear customer,");
		Label_1.setForeground(new Color(205, 92, 92));
		Label_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		Label_1.setBounds(202, 55, 194, 41);
		contentPane.add(Label_1);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(32, 185, 492, 316);
		contentPane.add(scrollPane);
		
		textArea = new JTextArea();
		textArea.setFont(new Font("微软雅黑", Font.PLAIN, 17));
		scrollPane.setViewportView(textArea);
		
		 Label_2 = new JLabel("Please fill in your feedback information");
		Label_2.setForeground(new Color(205, 92, 92));
		Label_2.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		Label_2.setBounds(202, 109, 348, 41);
		contentPane.add(Label_2);
		
		JLabel lblNewLabel_1 = new JLabel("");
		lblNewLabel_1.setIcon(new ImageIcon(Feedback.class.getResource("/img/Feedback.jpg")));
		lblNewLabel_1.setBounds(14, 13, 143, 159);
		contentPane.add(lblNewLabel_1);
		
		btnSubmit = new JButton("Submit");
		btnSubmit.setFont(new Font("微软雅黑", Font.BOLD, 19));
		btnSubmit.setBounds(211, 541, 122, 33);
		contentPane.add(btnSubmit);
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				submit();
			}
		});
		
		btnBack = new JButton("Back");
		btnBack.setFont(new Font("微软雅黑", Font.PLAIN, 19));
		btnBack.setBounds(14, 558, 81, 33);
		contentPane.add(btnBack);
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Receipt(num, id).setVisible(true);
				dispose();
			}
		});
		
		comboBox = new JComboBox<>();						//组合按钮或可编辑字段和下拉列表的组件
		comboBox.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 17));
		comboBox.addItemListener(new ItemListener() {		//用于接收项目事件的侦听器接口。
			public void itemStateChanged(ItemEvent e) {
				int index = comboBox.getSelectedIndex();	//返回列表中与给定项目匹配的第一个项目。 
					String arr[] = listCountries.get(index).getId().split("-");					//  ???（应该是遇见 - 就分割）
					locale = new Locale(arr[0], arr[1]);//	???   Locale对象代表具体的地理，政治或文化地区。（第一个是语言，第二个是国家）
					rd = ResourceBundle.getBundle("resources.content", locale);		//使用指定的基本名称，区域设置和类加载器获取资源包（这里好像没用类加载器）。 
					try {
						changeLanguage();
					} catch (Exception e1) {
						e1.printStackTrace();
					}
			}
		});
		comboBox.setBounds(419, 14, 98, 32);
		contentPane.add(comboBox);
		
		fillCountries();
	}

	protected void submit() {
		// TODO Auto-generated method stub
		DbUtil dbUtil = new DbUtil();
		Connection con = null;
		
		try {
			con = dbUtil.getCon();
			Orders orders = OrdersDao.OrdersSearch(con, Id);
			
			if(orders==null)
			{
				JOptionPane.showMessageDialog(null, "请输入正确的订单号！");
				return;
			}
			
			if(orders.getStatus()==2)
			{
				JOptionPane.showMessageDialog(null, "该订单您已经评价过来，请勿重复评价！");
				return;
			}
			
			OrdersDao.OrdersUpdate_st(con, orders.getId(), textArea.getText());
			JOptionPane.showMessageDialog(null, "评价成功！");
			dispose();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void fillCountries() {
		// TODO Auto-generated method stub
		listCountries = new ArrayList<Country>();
		listCountries.add(new Country("en-US", "English"));
		listCountries.add(new Country("zh-CN", "中文"));
		
		for(Country c : listCountries)
			comboBox.addItem(c.getName());
	}

	protected void changeLanguage() {
		// TODO Auto-generated method stub
		
		Label_1.setText(rd.getString("Label_1"));
		Label_2.setText(rd.getString("Label_2"));
		btnSubmit.setText(rd.getString("btnSubmit"));
		btnBack.setText(rd.getString("btnBack"));
	}
}
